package com.sztest.jayjun;

import android.app.Application;
import android.os.Handler;

import com.sztest.jayjun.mmkv.MMKVUtils;
import com.sztest.jayjun.utils.ADCThread;
import com.sztest.jayjun.utils.FileIOUtils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;

import pri.tool.pump.PumpInterface;

/*
 *  @项目名：  jayjun
 *  @包名：    com.sztest.jayjun
 *  @文件名:   MyApplication
 *  @创建者:   okmin
 *  @创建时间:  2022/6/28 9:00
 *  @描述：    TODO
 */
public class MyApplication extends Application {
    private PumpInterface mBump = null;

    public Handler mHandler;
    private static MyApplication sInstence;
    private ADCThread mAdcThread;
    private ExecutorService mWorkerThread;

    public static MyApplication getInstance() {
        return sInstence;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstence = this;
        mHandler = new Handler();
        MMKVUtils.getInstance().init();
//        mAdcThread = new ADCThread();
//        mAdcThread.start();
    }

    public ADCThread getAdcThread() {
        return mAdcThread;
    }
//
//    protected PumpInterface getPumpInterface() {
//        if (mBump == null) {
//            mBump = new PumpInterface();
//        }
//        return mBump;
//    }

    protected PumpInterface getPumpInterface2() {
        if (mBump == null) {
            mBump = new PumpInterface();
        }
        return mBump;
    }

    boolean jniIsThread=false;
    public int OnOffBuzzer(int isOn) {
        if (jniIsThread) {
            if (mWorkerThread!=null){
                mWorkerThread.submit(new Runnable() {
                    @Override
                    public void run() {
                        getPumpInterface2().OnOffBuzzer(isOn);
                    }
                });
            }
        }else {
            getPumpInterface2().OnOffBuzzer(isOn);
        }
        return 0;
    }

    public int OnOffLed(int isOn) {
        getPumpInterface2().OnOffLed(isOn);
        
        return 0;
    }

    public int OnOffPump(int isOn) {
        getPumpInterface2().OnOffPump(isOn);
        
        return 0;
    }

    public int OnOffAir(int isOn) {
        getPumpInterface2().OnOffAir(isOn);
        
        return 0;
    }

    public int OnOffMotor(int isOn) {//马达
        getPumpInterface2().OnOffMotor(isOn==1?0:1);
//        getPumpInterface2().OnOffMotor(isOn);

        return 0;
    }

    public int SetMotorDirect(int dir) {
        getPumpInterface2().SetMotorDirect(dir);
        
        return 0;
    }
    public int  get_mPa(){
       return getPumpInterface2().get_mPa();
    }
    // FFFFFF 开始 0是停止
    public int setMotorStep(int freq, int step) {
        getPumpInterface2().setMotorStep(freq,step);
        
        return 0;
    }

    // FFFFFF 开始 0是停止
    public int setMotorStep2(int freq, int step) {
        getPumpInterface2().setMotorStep2(freq,step);

        return 0;
    }

    public String execRootCmd(String cmd) {
//        LogUtils.e("init","cmd="+cmd);
        String result = "";
        DataOutputStream dos = null;
        DataInputStream dis = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dis = new DataInputStream(p.getInputStream());

            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            String line = null;

            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
            while ((line = br.readLine()) != null) {
                result += line;
            }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public void writeFileString(File file, String content) {
        FileIOUtils.writeFileFromString(file, content, false);
    }

    public String readFileString(File file) {
        return FileIOUtils.readFile2String(file);
    }


    public void setWorkerThread(ExecutorService workerThread) {
        mWorkerThread = workerThread;
    }

    public ExecutorService getWorkerThread() {
        return mWorkerThread;
    }
}
